<!--

    Copyright © 2016-2025 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section [formGroup]="credentialsConfigFormGroup" class="flex flex-col">
  <mat-expansion-panel class="tb-credentials-config-panel-group" expanded="true">
    <mat-expansion-panel-header>
      <mat-panel-title translate>rule-node-config.credentials</mat-panel-title>
      <mat-panel-description>
        {{ credentialsTypeTranslationsMap.get(credentialsConfigFormGroup.get('type').value) | translate }}
      </mat-panel-description>
    </mat-expansion-panel-header>
    <ng-template matExpansionPanelContent>
      <mat-form-field class="mat-block">
        <mat-label translate>rule-node-config.credentials-type</mat-label>
        <mat-select formControlName="type" required>
          <mat-option *ngFor="let credentialsType of allCredentialsTypes" [value]="credentialsType"
                      [disabled]="credentialsType === 'cert.PEM' && disableCertPemCredentials">
            {{ credentialsTypeTranslationsMap.get(credentialsType) | translate }}
          </mat-option>
        </mat-select>
        <mat-error *ngIf="credentialsConfigFormGroup.get('type').hasError('required')">
          {{ 'rule-node-config.credentials-type-required' | translate }}
        </mat-error>
      </mat-form-field>
      <section class="flex flex-col" [ngSwitch]="credentialsConfigFormGroup.get('type').value">
        <ng-template ngSwitchCase="anonymous">
        </ng-template>
        <ng-template ngSwitchCase="basic">
          <mat-form-field class="mat-block">
            <mat-label translate>rule-node-config.username</mat-label>
            <input required matInput formControlName="username">
            <mat-error *ngIf="credentialsConfigFormGroup.get('username').hasError('required')">
              {{ 'rule-node-config.username-required' | translate }}
            </mat-error>
          </mat-form-field>
          <mat-form-field class="mat-block">
            <mat-label translate>rule-node-config.password</mat-label>
            <input type="password" [required]="passwordFieldRequired" matInput formControlName="password">
            <tb-toggle-password matSuffix></tb-toggle-password>
            <mat-error *ngIf="credentialsConfigFormGroup.get('password').hasError('required')">
              {{ 'rule-node-config.password-required' | translate }}
            </mat-error>
          </mat-form-field>
        </ng-template>
        <ng-template ngSwitchCase="cert.PEM">
          <div class="tb-hint">{{ 'rule-node-config.credentials-pem-hint' | translate }}</div>
          <tb-file-input formControlName="caCert"
                         inputId="caCertSelect"
                         [existingFileName]="credentialsConfigFormGroup.get('caCertFileName').value"
                         (fileNameChanged)="credentialsConfigFormGroup.get('caCertFileName').setValue($event)"
                         label="{{'rule-node-config.ca-cert' | translate}}"
                         noFileText="rule-node-config.no-file"
                         dropLabel="{{'rule-node-config.drop-file' | translate}}">
          </tb-file-input>
          <tb-file-input formControlName="cert"
                         inputId="CertSelect"
                         [existingFileName]="credentialsConfigFormGroup.get('certFileName').value"
                         (fileNameChanged)="credentialsConfigFormGroup.get('certFileName').setValue($event)"
                         label="{{'rule-node-config.cert' | translate}}"
                         noFileText="rule-node-config.no-file"
                         dropLabel="{{'rule-node-config.drop-file' | translate}}">
          </tb-file-input>
          <tb-file-input style="padding-bottom: 8px;"
                         formControlName="privateKey"
                         inputId="privateKeySelect"
                         [existingFileName]="credentialsConfigFormGroup.get('privateKeyFileName').value"
                         (fileNameChanged)="credentialsConfigFormGroup.get('privateKeyFileName').setValue($event)"
                         label="{{'rule-node-config.private-key' | translate}}"
                         noFileText="rule-node-config.no-file"
                         dropLabel="{{'rule-node-config.drop-file' | translate}}">
          </tb-file-input>
          <mat-form-field class="mat-block">
            <mat-label translate>rule-node-config.private-key-password</mat-label>
            <input type="password" matInput formControlName="password">
            <tb-toggle-password matSuffix></tb-toggle-password>
          </mat-form-field>
        </ng-template>
      </section>
    </ng-template>
  </mat-expansion-panel>
</section>
